﻿function DebugPrint(...)
	if GRASaves.debugPrint then
		print(...);
	end
end

-- Create main frame
GodlikeRaidAssistant = CreateFrame("Frame", "GodlikeRaidAssistant", UIParent);
-- set event handler to respective function, eg: ADDON_LOADED will be GodlikeRaidAssistant:ADDON_LOADED
GodlikeRaidAssistant:SetScript('OnEvent', function(_, event, ...) return GodlikeRaidAssistant[event](GodlikeRaidAssistant, event, ...) end)
-- Register ADDON_LOADED event to do some on load command
GodlikeRaidAssistant:RegisterEvent("ADDON_LOADED");

-- default settings
GodlikeRaidAssistant.Default = {
	debugPrint = false,
	lackGearChecked = false,
	bossesNeeded = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
	-- 2 for need, 1 for greed, 0 for none
};

GodlikeRaidAssistant.currentTab = "ChooseLackGearTabText";

-- Get version information from .toc
local VERSION = GetAddOnMetadata("GodlikeRaidAssistant", "Version");
local CLASS_COLORCODE = {
	"|cffc79c6e",	-- Warrior
	"|cffF58cba",	-- Paladin
	"|cffabd473",	-- Hunter
	"|cfffff569",	-- Rogue
	"|cffffffff",	-- Priest
	"|cffc41f3b",	-- Death Knight
	"|cff0070de",	-- Shaman
	"|cff69ccf0",	-- Mage
	"|cff9482c9",	-- Warlock
	"|cff00ff96",	-- Monk
	"|cffff7d0a",	-- Druid
};


GRASaves = {};
GRASaves.debugPrint = GodlikeRaidAssistant.Default.debugPrint;

GRAGuildSaves = {};
GRAGuildSaves["members"] = {};

RegisterAddonMessagePrefix("GRAcheckversion");
RegisterAddonMessagePrefix("GRAlackrequest");
RegisterAddonMessagePrefix("GRAlackresponse")

function GodlikeRaidAssistant:Loadinginfo()
	print("GodlikeRaidAssistant " .. VERSION .. " " .. GRALocals["Loaded"]);
end

SLASH_GODLIKERAIDASSISTANT1 = "/gra"
SlashCmdList["GODLIKERAIDASSISTANT"] = function(msg)
	local ary = {}
	local i = 0
	for v in msg:gmatch("%S+") do
		ary[i] = v		
		--DEFAULT_CHAT_FRAME:AddMessage(ary[i])
		i = i+1
	end
	
	GodlikeRaidAssistant:Show();
end

function GodlikeRaidAssistant:ADDON_LOADED(_, name)
	if name == "GodlikeRaidAssistant" and not self.initialized then
		self:UnregisterEvent("ADDON_LOADED");
		self:RegisterEvent("VARIABLES_LOADED");
		self:RegisterEvent("CHAT_MSG_ADDON");
		self.initialized = true;
		DebugPrint("Debug: Addon loaded");
		
		self:Loadinginfo();
	end
end

function GodlikeRaidAssistant:VARIABLES_LOADED(_)
	local v, t;
	for v,t in pairs(self.Default) do
		if GRASaves[v]==nil or type(GRASaves[v])~=type(t) then
			DebugPrint(v,t);
			GRASaves[v] = t;
		end
	end
	
	self:CreateGodlikeRaidAssistantFrame();
end

function GodlikeRaidAssistant:CHAT_MSG_ADDON(_, prefix, message, distributionType, sender)
	if prefix == "GRAlackrequest" then
		DebugPrint("lack requested");
		local returnString = "";
		local i;
		for i = 1, 13 do
			returnString = returnString .. GRASaves.bossesNeeded[i];
		end
		DebugPrint(returnString);
		SendAddonMessage("GRAlackresponse", returnString, "WHISPER", sender);
	elseif prefix == "GRAlackresponse" then
		DebugPrint("Get " .. sender);
		GRAGuildSaves[sender] = {};
		GRAGuildSaves[sender].lackGear = {};
		local i;
		for i = 1, 13 do
			if GRAGuildSaves.members[sender] == nil then
				GRAGuildSaves.members[sender] = {};
			end
			if GRAGuildSaves.members[sender].lackGear == nil then
				GRAGuildSaves.members[sender].lackGear = {};
			end
			GRAGuildSaves.members[sender].lackGear[i] = tonumber(string.sub(message, i, i));
			
		end
		GRAGuildSaves[sender].class = UnitClass(sender);
	end
end

local FrameBackdrop = {
	bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
	edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
	tile = true, tileSize = 32, edgeSize = 32,
	insets = { left = 8, right = 8, top = 8, bottom = 8 }
}

local PaneBackdrop  = {
	bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
	tile = true, tileSize = 16, edgeSize = 16,
	insets = { left = 3, right = 3, top = 5, bottom = 3 }
}

local function SwitchToChooseLackGearTab()
	GodlikeRaidAssistant.chooseLackGearTab:SetBackdropColor(unpack(GodlikeRaidAssistant.selectedTabColor));
	GodlikeRaidAssistant.controlTab:SetBackdropColor(unpack(GodlikeRaidAssistant.unselectedTabColor));
	GodlikeRaidAssistant.guildTab:SetBackdropColor(unpack(GodlikeRaidAssistant.unselectedTabColor));
	GodlikeRaidAssistant.currentTab = "ChooseLackGearTab";
	GodlikeRaidAssistant.mainWindow.chooseLackGearWindow:Show();
	GodlikeRaidAssistant.mainWindow.controlWindow:Hide();
	GodlikeRaidAssistant.mainWindow.guildWindow:Hide();
end

local function SwitchToControlTab()
	GodlikeRaidAssistant.chooseLackGearTab:SetBackdropColor(unpack(GodlikeRaidAssistant.unselectedTabColor));
	GodlikeRaidAssistant.controlTab:SetBackdropColor(unpack(GodlikeRaidAssistant.selectedTabColor));
	GodlikeRaidAssistant.guildTab:SetBackdropColor(unpack(GodlikeRaidAssistant.unselectedTabColor));
	GodlikeRaidAssistant.currentTab = "ControlTab";
	GodlikeRaidAssistant.mainWindow.chooseLackGearWindow:Hide();
	GodlikeRaidAssistant.mainWindow.controlWindow:Show();
	GodlikeRaidAssistant.mainWindow.guildWindow:Hide();
end

local function SwitchToGuildTab()
	GodlikeRaidAssistant.chooseLackGearTab:SetBackdropColor(unpack(GodlikeRaidAssistant.unselectedTabColor));
	GodlikeRaidAssistant.controlTab:SetBackdropColor(unpack(GodlikeRaidAssistant.unselectedTabColor));
	GodlikeRaidAssistant.guildTab:SetBackdropColor(unpack(GodlikeRaidAssistant.selectedTabColor));
	GodlikeRaidAssistant.currentTab = "GuildTab";
	GodlikeRaidAssistant.mainWindow.chooseLackGearWindow:Hide();
	GodlikeRaidAssistant.mainWindow.controlWindow:Hide();
	GodlikeRaidAssistant.mainWindow.guildWindow:Show();
end

local function ClickBossPane(self, button, down)
	if GRASaves.bossesNeeded[self.bossID] == 2 then
		-- switch to greed
		self:SetBackdropColor(0.7, 0.7, 0.7);
		GRASaves.bossesNeeded[self.bossID] = 1;
	elseif GRASaves.bossesNeeded[self.bossID] == 1 then
		-- switch to no
		self:SetBackdropColor(0.1, 0.1, 0.1);
		GRASaves.bossesNeeded[self.bossID] = 0;
	else
		-- switch to need
		self:SetBackdropColor(1.0, 0.0, 0.0);
		GRASaves.bossesNeeded[self.bossID] = 2;
	end
end

local function RefreshLackGearList()
--[[
	local i;
	for i = 1, 13 do
		local lackPeople = "";
		for v,t in pairs(GRAGuildSaves) do
			if GRAGuildSaves[v].lackGear[i] == true then
				lackPeople = lackPeople .. v .. " ";
			end
		end
		GodlikeRaidAssistant.mainWindow.controlWindow.bossControls[i].bossLackList:SetText(lackPeople);
	end
	--]]
end

local refreshLackGearListCountDownTime;
local function RefreshLackGearListCountDown(self, elapsed)
	refreshLackGearListCountDownTime = refreshLackGearListCountDownTime + elapsed;
	if refreshLackGearListCountDownTime >= 2.0 then
		RefreshLackGearList();	
		GodlikeRaidAssistant:SetScript("OnUpdate", nil);
	end
end

function GodlikeRaidAssistant:RefreshGuildInfo()
	--if GRAGuildSaves["members"] == nil then
	--	return
	--end
	
	for i = 1, 11 do
		for j = 1, 6 do
			GodlikeRaidAssistant.mainWindow.guildWindow.classPanes[i].members[j]:Hide();
		end
	end
	
	local classCount = {};
	for i = 1, 11 do
		classCount[i] = 0;
	end
	for name, value in pairs(GRAGuildSaves["members"]) do
		
		classCount[value.class] = classCount[value.class]+1;
		if classCount[value.class] <= 6 then
			local memberFrame = GodlikeRaidAssistant.mainWindow.guildWindow.classPanes[value.class].members[classCount[value.class]];
			memberFrame:Show();
			memberFrame.memberNameText:SetText(name);
			local id, specName = GetSpecializationInfoByID(value.spec);
			memberFrame.memberSpecText:SetText(specName); 
			if value.alt == true then
				memberFrame.memberAltText:SetText(GRALocals["AltSwitchAltText"]);
			else
				memberFrame.memberAltText:SetText(GRALocals["AltSwitchMainText"]);
			end
		end
	end
end

function AddMember()
	if GRAGuildSaves["members"] == nil then
		GRAGuildSaves["members"] = {};
	end
	
	local memberName = _G["memberNameEditBox"]:GetText();
	local memberClass = _G["memberClassDropDownMenu"].classID;
	local memberSpec = _G["memberSpecDropDownMenu"].specID;
	local memberAlt = _G["altSwitchButton"].alt;
	local memberMain = _G["altMainNameEditBox"]:GetText();
	
	DebugPrint(memberName, memberClass, memberSpec, memberAlt, memberMain);
	
	GRAGuildSaves["members"][memberName] = {};
	GRAGuildSaves["members"][memberName].class = memberClass;
	GRAGuildSaves["members"][memberName].spec = memberSpec;
	GRAGuildSaves["members"][memberName].alt = memberAlt;
	GRAGuildSaves["members"][memberName].main = memberMain;
	
	GodlikeRaidAssistant:RefreshGuildInfo();
end

function GodlikeRaidAssistant:CreateGodlikeRaidAssistantFrame()
	-----------------------
	--  Main Frame
	-----------------------
	GodlikeRaidAssistant:Hide();
	GodlikeRaidAssistant:EnableMouse(true);
	GodlikeRaidAssistant:SetMovable(true);
	GodlikeRaidAssistant:RegisterForDrag("LeftButton");
	GodlikeRaidAssistant:SetResizable(true);
	GodlikeRaidAssistant:SetFrameStrata("FULLSCREEN_DIALOG");
	GodlikeRaidAssistant:SetBackdrop(FrameBackdrop);
	GodlikeRaidAssistant:SetBackdropColor(0.1, 0.1, 0.1);
	GodlikeRaidAssistant:SetBackdropBorderColor(0.4, 0.4, 0.4);
	GodlikeRaidAssistant:SetPoint("CENTER");
	GodlikeRaidAssistant:SetMinResize(400, 200);
	GodlikeRaidAssistant:SetToplevel(true);
	GodlikeRaidAssistant:SetWidth(800);
	GodlikeRaidAssistant:SetHeight(800);
	GodlikeRaidAssistant:SetScript("OnDragStart", GodlikeRaidAssistant.StartMoving);
	GodlikeRaidAssistant:SetScript("OnDragStop", GodlikeRaidAssistant.StopMovingOrSizing);
	
	GodlikeRaidAssistant.selectedTabColor = {0.1, 0.1, 0.1};
	GodlikeRaidAssistant.unselectedTabColor = {0.1, 0.1, 0.5};

	local closeButton = CreateFrame("Button", nil, GodlikeRaidAssistant, "UIPanelButtonTemplate");
	closeButton:SetScript("OnClick", function()
		GodlikeRaidAssistant:Hide();
	end);
	closeButton:SetPoint("BOTTOMRIGHT", -27, 17);
	closeButton:SetHeight(20);
	closeButton:SetWidth(100);
	closeButton:SetText(CLOSE);

	local refreshButton = CreateFrame("Button", nil, GodlikeRaidAssistant, "UIPanelButtonTemplate");
	refreshButton:SetScript("OnClick", function()
		DebugPrint("Send update request");
		SendAddonMessage("GRAlackrequest", "", "RAID");
		refreshLackGearListCountDownTime = 0.0;
		GodlikeRaidAssistant:SetScript("OnUpdate", RefreshLackGearListCountDown);
	end);
	refreshButton:SetPoint("BOTTOMRIGHT", -127, 17);
	refreshButton:SetHeight(20);
	refreshButton:SetWidth(100);
	refreshButton:SetText(GRALocals["RefreshData"]);

	local chooseLackGearTab = CreateFrame("Button", nil, GodlikeRaidAssistant);
	GodlikeRaidAssistant.chooseLackGearTab = chooseLackGearTab;
	chooseLackGearTab:SetPoint("TOPLEFT", GodlikeRaidAssistant, "TOPLEFT", 20, -20);
	chooseLackGearTab:SetWidth(100);
	chooseLackGearTab:SetHeight(25);
	chooseLackGearTab:SetBackdrop(PaneBackdrop);
	chooseLackGearTab:SetBackdropColor(unpack(GodlikeRaidAssistant.selectedTabColor));
	chooseLackGearTab:SetBackdropBorderColor(0.9, 0.9, 0.9);
	chooseLackGearTab:SetScript("OnClick", SwitchToChooseLackGearTab);
	
	local chooseLackGearTabText = chooseLackGearTab:CreateFontString("ChooseLackGearTabText");
	chooseLackGearTabText:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
	chooseLackGearTabText:SetText(GRALocals["ChooseLackGearTabText"]);
	chooseLackGearTabText:SetPoint("Center", chooseLackGearTab);
	
	local controlTab = CreateFrame("Button", nil, GodlikeRaidAssistant);
	GodlikeRaidAssistant.controlTab = controlTab;
	controlTab:SetPoint("TOPLEFT", GodlikeRaidAssistant, "TOPLEFT", 120, -20);
	controlTab:SetWidth(100);
	controlTab:SetHeight(25);
	controlTab:SetBackdrop(PaneBackdrop);
	controlTab:SetBackdropColor(unpack(GodlikeRaidAssistant.unselectedTabColor));
	controlTab:SetBackdropBorderColor(0.9, 0.9, 0.9);
	controlTab:SetScript("OnClick", SwitchToControlTab);
	
	local controlTabText = controlTab:CreateFontString("ControlTabText");
	controlTabText:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
	controlTabText:SetText(GRALocals["ControlTabText"]);
	controlTabText:SetPoint("Center", controlTab);

	local guildTab = CreateFrame("Button", nil, GodlikeRaidAssistant);
	GodlikeRaidAssistant.guildTab = guildTab;
	guildTab:SetPoint("TOPLEFT", GodlikeRaidAssistant, "TOPLEFT", 220, -20);
	guildTab:SetWidth(100);
	guildTab:SetHeight(25);
	guildTab:SetBackdrop(PaneBackdrop);
	guildTab:SetBackdropColor(unpack(GodlikeRaidAssistant.unselectedTabColor));
	guildTab:SetBackdropBorderColor(0.9, 0.9, 0.9);
	guildTab:SetScript("OnClick", SwitchToGuildTab);
	
	local guildTabText = guildTab:CreateFontString("GuildTabText");
	guildTabText:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
	guildTabText:SetText(GRALocals["GuildTabText"]);
	guildTabText:SetPoint("Center", guildTab);

	
	local mainWindow = CreateFrame("Frame", nil, GodlikeRaidAssistant);
	GodlikeRaidAssistant.mainWindow = mainWindow;
	mainWindow:SetPoint("Center", GodlikeRaidAssistant, 0, -5);
	mainWindow:SetWidth(780);
	mainWindow:SetHeight(710);
	mainWindow:SetBackdrop(PaneBackdrop);
	mainWindow:SetBackdropColor(0.1, 0.1, 0.1);
	mainWindow:SetBackdropBorderColor(0.9, 0.9, 0.9);
	
	-----------------------
	--  Choose boss tab
	-----------------------	
	local chooseLackGearWindow = CreateFrame("Frame", nil, mainWindow);
	mainWindow.chooseLackGearWindow = chooseLackGearWindow;
	chooseLackGearWindow:SetAllPoints(mainWindow);
	
	local bossPanes = {};
	local i = 1;
	for i = 1, 13 do
		local widthBetweenFrame, heightBetweenFrame = 13, 10;
		local frameWidth, frameHeight = 140, 200;
		bossPanes[i] = CreateFrame("Button", nil, chooseLackGearWindow);
		if i < 4 then
			bossPanes[i]:SetPoint("TOPLEFT", chooseLackGearWindow, "TOPLEFT", widthBetweenFrame, -(heightBetweenFrame + (i-1)*(heightBetweenFrame+frameHeight)));
			bossPanes[i]:SetWidth(frameWidth);
			bossPanes[i]:SetHeight(frameHeight);
		elseif i >= 4 and i < 7 then
			bossPanes[i]:SetPoint("TOPLEFT", chooseLackGearWindow, "TOPLEFT", 2*widthBetweenFrame + frameWidth, -(heightBetweenFrame + (i-4)*(heightBetweenFrame+frameHeight)));
			bossPanes[i]:SetWidth(frameWidth);
			bossPanes[i]:SetHeight(frameHeight);
		elseif i >= 7 and i < 10 then
			bossPanes[i]:SetPoint("TOPLEFT", chooseLackGearWindow, "TOPLEFT", 3*widthBetweenFrame + 2*frameWidth, -(heightBetweenFrame + (i-7)*(heightBetweenFrame+frameHeight)));
			bossPanes[i]:SetWidth(frameWidth);
			bossPanes[i]:SetHeight(frameHeight);
		elseif i >= 10 and i < 13 then
			bossPanes[i]:SetPoint("TOPLEFT", chooseLackGearWindow, "TOPLEFT", 4*widthBetweenFrame + 3*frameWidth, -(heightBetweenFrame + (i-10)*(heightBetweenFrame+frameHeight)));
			bossPanes[i]:SetWidth(frameWidth);
			bossPanes[i]:SetHeight(frameHeight);
		elseif i == 13 then
			bossPanes[i]:SetPoint("TOPLEFT", chooseLackGearWindow, "TOPLEFT", 5*widthBetweenFrame + 4*frameWidth, -heightBetweenFrame);
			bossPanes[i]:SetWidth(frameWidth);
			bossPanes[i]:SetHeight(frameHeight);		
		end
		bossPanes[i]:SetBackdrop(PaneBackdrop);
		
		DebugPrint("Bosses needed " .. i .. ": ", GRASaves.bossesNeeded[i]);
		if GRASaves.bossesNeeded[i] == 2 then
			bossPanes[i]:SetBackdropColor(1.0, 0.0, 0.0);
		elseif GRASaves.bossesNeeded[i] == 1 then
			bossPanes[i]:SetBackdropColor(0.7, 0.7, 0.7);
		else
			bossPanes[i]:SetBackdropColor(0.1, 0.1, 0.1);
		end
		bossPanes[i]:SetBackdropBorderColor(0.9, 0.9, 0.9);

		local bossPanesText = bossPanes[i]:CreateFontString("bossPanesText" .. i);
		bossPanesText:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
		bossPanesText:SetText(GRALocals["BossModelText"][i]);
		bossPanesText:SetPoint("Top", bossPanes[i], "Top", 0, -10);

		local displayID = {47552, 47325, 47229, 46559, 47414, 46675, 47189, 47009, 47527, 46627, 46975, 46770, 47739};
		local creatureID = {69465, 68476, 69134, 67977, 70212, 69712, 68036, 69017, 69427, 68078, 68905, 68397, 69473};
		local bossModel = CreateFrame("PlayerModel", nil, bossPanes[i]);
		bossModel:SetWidth(frameWidth);
		bossModel:SetHeight(frameHeight-20);
		bossModel:SetDisplayInfo(displayID[i]);
		bossModel:SetCreature(creatureID[i]);
		bossModel:SetPoint("Center", bossPanes[i]);
		
		bossPanes[i].bossID = i;
		bossPanes[i]:SetScript("OnClick", ClickBossPane);
	end
	chooseLackGearWindow.bossPanes = bossPanes;
	local chooseBossTabTooltipText = chooseLackGearWindow:CreateFontString("chooseBossTabTooltipText");
	chooseBossTabTooltipText:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
	chooseBossTabTooltipText:SetText(GRALocals["ChooseBossTabTooltipText"]);
	chooseBossTabTooltipText:SetPoint("BOTTOMLEFT", chooseLackGearWindow, "BOTTOMLEFT", 5, 5);
	
	-----------------------
	--  Raid control tab
	-----------------------	
	local controlWindow =  CreateFrame("Frame", nil, mainWindow);
	mainWindow.controlWindow = controlWindow;
	controlWindow:SetAllPoints(mainWindow);
	controlWindow:Hide();
	
	local bossControls = {};
	for i = 1, 13 do
		local heightBetweenFrame = 4;
		local frameWidth, frameHeight = 650, 50;
		bossControls[i] = CreateFrame("Button", nil, controlWindow);
		bossControls[i]:SetPoint("TOP", controlWindow, "TOP", 0, -(heightBetweenFrame + (i-1)*(heightBetweenFrame+frameHeight)));
		bossControls[i]:SetWidth(frameWidth);
		bossControls[i]:SetHeight(frameHeight);
		bossControls[i]:SetBackdrop(PaneBackdrop);
		bossControls[i]:SetBackdropColor(0.1, 0.1, 0.1);

		local bossControlText = bossControls[i]:CreateFontString("bossControlText" .. i);
		bossControlText:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
		bossControlText:SetText(GRALocals["BossModelText"][i]);
		bossControlText:SetPoint("TOPLEFT", bossControls[i], "TOPLEFT", 50, -5);

		local bossLackListFrame = CreateFrame("Button", nil, bossControls[i]);
		bossLackListFrame:SetPoint("TOPLEFT", bossControls[i], "TOPLEFT", 50, -20);
		bossLackListFrame:SetPoint("BOTTOMRIGHT", bossControls[i]);
		
		local bossLackList = bossControls[i]:CreateFontString("bossLackList" .. i);
		bossLackList:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
		bossLackList:SetText(GRALocals["BossModelText"][i]);
		bossLackList:SetPoint("TOPLEFT", bossControls[i], "TOPLEFT", 50, -20);
		bossLackList:SetPoint("RIGHT", bossControls[i]);
		bossLackList:SetJustifyH("LEFT");
		bossLackList:SetJustifyV("TOP");
		bossControls[i].bossLackList = bossLackList;
		
		local displayID = {47552, 47325, 47229, 46559, 47414, 46675, 47189, 47009, 47527, 46627, 46975, 46770, 47739};
		local creatureID = {69465, 68476, 69134, 67977, 70212, 69712, 68036, 69017, 69427, 68078, 68905, 68397, 69473};
		local bossModel = CreateFrame("PlayerModel", nil, bossControls[i]);
		bossModel:SetWidth(frameHeight);
		bossModel:SetHeight(frameHeight);
		bossModel:SetDisplayInfo(displayID[i]);
		bossModel:SetCreature(creatureID[i]);
		bossModel:SetPoint("LEFT", bossControls[i], "LEFT", 0, 0);
		
		--bossControls[i].bossID = i;
		--bossControls[i]:SetScript("OnClick", ClickBossPane);
	end
	controlWindow.bossControls = bossControls;
	RefreshLackGearList();
	
	-----------------------
	--  Guild control tab
	-----------------------	
	local guildWindow =  CreateFrame("Frame", nil, mainWindow);
	mainWindow.guildWindow = guildWindow;
	guildWindow:SetAllPoints(mainWindow);
	guildWindow:Hide();
	
	local classPanes = {};
	local widthBetweenFrame, heightBetweenFrame = 10, 10;
	local frameWidth, frameHeight = 100, 340;
	for i = 1, 11 do
		classPanes[i] = CreateFrame("Frame", nil, guildWindow);
		if i < 7 then
			classPanes[i]:SetPoint("TOPLEFT", guildWindow, "TOPLEFT", widthBetweenFrame+(i-1)*(widthBetweenFrame+frameWidth), -heightBetweenFrame);
		else
			classPanes[i]:SetPoint("TOPLEFT", guildWindow, "TOPLEFT", widthBetweenFrame+(i-7)*(widthBetweenFrame+frameWidth), -2*heightBetweenFrame-frameHeight);
		end
		classPanes[i]:SetWidth(frameWidth);
		classPanes[i]:SetHeight(frameHeight);
		classPanes[i]:SetBackdrop(PaneBackdrop);
		classPanes[i]:SetBackdropColor(0.1, 0.1, 0.1);
		
		local classPaneText = classPanes[i]:CreateFontString("classPaneText" .. i);
		classPaneText:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
		local classDisplayName = GetClassInfo(i);
		DebugPrint(classDisplayName);
		classPaneText:SetText(CLASS_COLORCODE[i] .. classDisplayName .. "|r");
		classPaneText:SetPoint("TOP", classPanes[i], "TOP", 0, -5);
		
		local j;
		local members = {};
		for j = 1, 6 do
			local heightOverhead = 20;
			local widthBetweenFrame, heightBetweenFrame = 5, 2;
			local frameWidth, frameHeight = 90, 50;
			members[j] = CreateFrame("Frame", nil, guildWindow);
			members[j]:SetPoint("TOPLEFT", classPanes[i], "TOPLEFT", widthBetweenFrame, -heightBetweenFrame - (j-1)*(heightBetweenFrame+frameHeight) - heightOverhead);
			members[j]:SetWidth(frameWidth);
			members[j]:SetHeight(frameHeight);
			members[j]:SetBackdrop(PaneBackdrop);
			members[j]:SetBackdropColor(0.1, 0.1, 0.1);
			members[j].UpdateMember = function()
				GRAGuildSaves.members[members[j].memberNameText:GetText()].alt = members[j].altSwitchButton.alt;
				GRAGuildSaves.members[members[j].memberNameText:GetText()].main = GRAGuildSaves.members[members[j].memberNameText:GetText()].altMainNameEditBox:GetText();
			end
			
			local memberNameText = members[j]:CreateFontString("memberNameText" .. j);
			memberNameText:SetFont("Fonts\\bHEI01B.TTF", 10, "OUTLINE, MONOCHROME");
			memberNameText:SetPoint("TOP", members[j], "TOP", 0, -5);
			--memberNameText:SetText("WWWWWWWWWWWW");
			members[j].memberNameText = memberNameText;

			local memberSpecText = members[j]:CreateFontString("memberSpecText" .. j);
			memberSpecText:SetFont("Fonts\\bHEI01B.TTF", 10, "OUTLINE, MONOCHROME");
			memberSpecText:SetPoint("TOP", memberNameText, "BOTTOM", 0, -3);
			memberSpecText:SetPoint("LEFT", members[j], "LEFT", 5, 0);
			--memberSpecText:SetText("野性戰鬥");
			members[j].memberSpecText = memberSpecText;

			local memberAltText = members[j]:CreateFontString("memberAltText" .. j);
			memberAltText:SetFont("Fonts\\bHEI01B.TTF", 10, "OUTLINE, MONOCHROME");
			memberAltText:SetPoint("TOP", memberNameText, "BOTTOM", 0, -3);
			memberAltText:SetPoint("RIGHT", members[j], "RIGHT", -5, 0);
			--memberAltText:SetText("本尊");
			members[j].memberAltText = memberAltText;

			local memberDeleteButton = CreateFrame("Button", nil, members[j], "UIPanelButtonTemplate");
			memberDeleteButton:SetScript("OnClick", function(self, button, down)
				DebugPrint("Remove", memberNameText:GetText());
				GRAGuildSaves.members[memberNameText:GetText()] = nil;
				GodlikeRaidAssistant:RefreshGuildInfo();
			end);
			memberDeleteButton:SetPoint("BOTTOMLEFT", members[j], "BOTTOMLEFT", 5, 5);
			memberDeleteButton:SetHeight(12);
			memberDeleteButton:SetWidth(50);
			memberDeleteButton:SetText(GRALocals["GuildTabDeleteMemberText"]);
			
			members[j]:Hide();
		end
		classPanes[i].members = members;
	end
	guildWindow.classPanes = classPanes;
	
	local addMemberText = guildWindow:CreateFontString("addMemeberText");
	addMemberText:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
	addMemberText:SetText(GRALocals["GuildTabAddMemberText"]);
	addMemberText:SetPoint("TOPLEFT", guildWindow, "TOPLEFT", 6*widthBetweenFrame+5*frameWidth, -2*heightBetweenFrame-frameHeight);
	
	local memberNameText = guildWindow:CreateFontString("memberNameText");
	memberNameText:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
	memberNameText:SetText(GRALocals["GuildTabMemberNameText"]);
	memberNameText:SetPoint("TOPLEFT", addMemberText, "BOTTOMLEFT", 10, -15);
	
	local memberNameEditBox = CreateFrame("EditBox", "memberNameEditBox", guildWindow, "InputBoxTemplate");
	memberNameEditBox:SetHeight(20);
	memberNameEditBox:SetWidth(150);
	memberNameEditBox:SetMaxLetters(12);
	memberNameEditBox:SetAutoFocus(false);
	memberNameEditBox:SetScript("OnEditFocusGained", function(self)
		self:SetTextColor(1, 1, 1);
	end);
	memberNameEditBox:SetScript("OnEditFocusLost", function(self)
		self:SetTextColor(0.5, 0.5, 0.5);
	end);
	memberNameEditBox:SetScript("OnEnterPressed", function(self)
		self:ClearFocus();
	end);
	memberNameEditBox:SetScript("OnEscapePressed", function(self)
		self:ClearFocus();
	end);
	memberNameEditBox:SetPoint("TOPLEFT", addMemberText, "BOTTOMRIGHT", -5, -8);
	guildWindow.memberNameEditBox = memberNameEditBox;
	
	local memberClassText = guildWindow:CreateFontString("memberClassText");
	memberClassText:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
	memberClassText:SetText(GRALocals["GuildTabMemberClassText"]);
	memberClassText:SetPoint("TOPLEFT", memberNameText, "BOTTOMLEFT", 0, -15);
	
	local memberClassDropDownMenu = CreateFrame("Frame", "memberClassDropDownMenu", guildWindow, "UIDropDownMenuTemplate");
	memberClassDropDownMenu:SetPoint("TOPLEFT", memberNameEditBox, "BOTTOMLEFT", -22, -7);
	memberClassDropDownMenu.menuList = {};
	for i = 1, 11 do
		local classDisplayName, _, classID = GetClassInfo(i);
		memberClassDropDownMenu.menuList[i] = {};
		memberClassDropDownMenu.menuList[i].text = classDisplayName;
		memberClassDropDownMenu.menuList[i].value = classID;
		memberClassDropDownMenu.menuList[i].colorCode = CLASS_COLORCODE[i];
		DebugPrint(classDisplayName, classID);
	end
	function memberClassDropDownMenu:initialize(level)
		--DebugPrint(self, self.displayMode, self.menuList);
		for index = 1, #(self.menuList) do
			local value = self.menuList[index]
			if (value.text) then
				value.index = index;
				value.func = function()
					_G["memberClassDropDownMenuText"]:SetText(value.colorCode .. value.text .. "|r");
					memberClassDropDownMenu.classID = value.value;
					DebugPrint(memberClassDropDownMenu.classID);
					
					_G["memberSpecDropDownMenuText"]:SetText("");
					local specs = GetNumSpecializationsForClassID(value.value);
					for i = 1, 4 do
						_G["memberSpecDropDownMenu"].menuList[i] = nil;
					end
					for i = 1, specs do
						local specId, specName = GetSpecializationInfoForClassID(value.value, i);
						_G["memberSpecDropDownMenu"].menuList[i] = {};
						_G["memberSpecDropDownMenu"].menuList[i].text = specName;
						_G["memberSpecDropDownMenu"].menuList[i].value = specId;
					end
				end
				UIDropDownMenu_AddButton( value, level );
				--DebugPrint(value.text, level);
			end
		end
	end
	_G["memberClassDropDownMenuButton"]:SetScript("OnClick", function()
		--DebugPrint(_G["memberClassDropDownMenu"]:GetName());
		UIDropDownMenu_Initialize(_G["memberClassDropDownMenu"], _G["memberClassDropDownMenu"].initialize, _G["memberClassDropDownMenu"].displayMode, nil, _G["memberClassDropDownMenu"].menuList);
		ToggleDropDownMenu(1, nil, _G["memberClassDropDownMenu"], _G["memberClassDropDownMenu"], 15, 5, _G["memberClassDropDownMenu"].menuList, _G["memberClassDropDownMenuButton"], nil);
	end);
	guildWindow.memberClassDropDownMenu = memberClassDropDownMenu;
			
	local memberSpecText = guildWindow:CreateFontString("memberSpecText");
	memberSpecText:SetFont("Fonts\\bHEI01B.TTF", 12, "OUTLINE, MONOCHROME");
	memberSpecText:SetText(GRALocals["GuildTabMemberSpecText"]);
	memberSpecText:SetPoint("TOPLEFT", memberClassText, "BOTTOMLEFT", 0, -20);

	local memberSpecDropDownMenu = CreateFrame("Frame", "memberSpecDropDownMenu", guildWindow, "UIDropDownMenuTemplate");
	memberSpecDropDownMenu:SetPoint("TOPLEFT", memberClassDropDownMenu, "BOTTOMLEFT", 0, 0);
	memberSpecDropDownMenu.menuList = {};
	function memberSpecDropDownMenu:initialize(level)
		--DebugPrint(self, self.displayMode, self.menuList);
		for index = 1, #(self.menuList) do
			local value = self.menuList[index]
			if (value.text) then
				value.index = index;
				value.func = function()
					_G["memberSpecDropDownMenuText"]:SetText(value.text);
					memberSpecDropDownMenu.specID = value.value;
					DebugPrint(memberSpecDropDownMenu.specID);
				end
				UIDropDownMenu_AddButton( value, level );
				--DebugPrint(value.text, level);
			end
		end
	end
	_G["memberSpecDropDownMenuButton"]:SetScript("OnClick", function()
		UIDropDownMenu_Initialize(_G["memberSpecDropDownMenu"], _G["memberSpecDropDownMenu"].initialize, _G["memberSpecDropDownMenu"].displayMode, nil, _G["memberSpecDropDownMenu"].menuList);
		ToggleDropDownMenu(1, nil, _G["memberSpecDropDownMenu"], _G["memberSpecDropDownMenu"], 15, 5, _G["memberSpecDropDownMenu"].menuList, _G["memberSpecDropDownMenuButton"], nil);
	end);
	guildWindow.memberSpecDropDownMenu = memberSpecDropDownMenu;

	local altMainNameEditBox = CreateFrame("EditBox", "altMainNameEditBox", guildWindow, "InputBoxTemplate");
	altMainNameEditBox:SetHeight(20);
	altMainNameEditBox:SetWidth(150);
	altMainNameEditBox:SetMaxLetters(12);
	altMainNameEditBox:SetAutoFocus(false);
	altMainNameEditBox:SetScript("OnEditFocusGained", function(self)
		self:SetTextColor(1, 1, 1);
	end);
	altMainNameEditBox:SetScript("OnEditFocusLost", function(self)
		self:SetTextColor(0.5, 0.5, 0.5);
	end);
	altMainNameEditBox:SetScript("OnEnterPressed", function(self)
		self:ClearFocus();
	end);
	altMainNameEditBox:SetScript("OnEscapePressed", function(self)
		self:ClearFocus();
	end);
	altMainNameEditBox:SetPoint("TOPLEFT", memberSpecDropDownMenu, "BOTTOMLEFT", 23, 0);
	altMainNameEditBox:Hide();
	guildWindow.altMainNameEditBox = altMainNameEditBox;						

	local altSwitchButton = CreateFrame("Button", "altSwitchButton", guildWindow, "UIPanelButtonTemplate");
	altSwitchButton.alt = false;
	altSwitchButton:SetText(GRALocals["AltSwitchMainText"]);
	altSwitchButton:SetScript("OnClick", function(self, button, down)
		if self.alt == true then
			self.alt = false;
			altMainNameEditBox:Hide();
			self:SetText(GRALocals["AltSwitchMainText"]);
		else
			self.alt = true;
			altMainNameEditBox:Show();
			self:SetText(GRALocals["AltSwitchAltText"]);					
		end
	end);
	altSwitchButton:SetPoint("TOPLEFT", memberSpecText, "BOTTOMLEFT", -15, -12);
	altSwitchButton:SetHeight(20);
	altSwitchButton:SetWidth(50);
	guildWindow.altSwitchButton = altSwitchButton;
			
	local memberAddButton = CreateFrame("Button", nil, guildWindow, "UIPanelButtonTemplate");
	memberAddButton:SetScript("OnClick", AddMember);
	memberAddButton:SetPoint("TOPLEFT", altSwitchButton, "BOTTOMLEFT", -5, -15);
	memberAddButton:SetHeight(20);
	memberAddButton:SetWidth(100);
	memberAddButton:SetText(GRALocals["GuildTabAddText"]);

	self:RefreshGuildInfo();
end